package com.yida.file.service;

import java.io.InputStream;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.yida.common.core.utils.file.FileTypeUtils;
import com.yida.system.api.domain.SysFile;

/**
 * FastDFS 文件存储
 * 
 * @author yida
 */
@Service
public class FastDfsSysFileServiceImpl implements ISysFileService {
    /**
     * 域名或本机访问地址
     */
    @Value("${fdfs.domain}")
    public String domain;

    @Autowired
    private FastFileStorageClient storageClient;

    /**
     * FastDfs文件上传接口
     * 
     * @param file 上传的文件
     * @return 访问地址
     * @throws Exception
     */
    @Override
    public String uploadFile(MultipartFile file) throws Exception {
        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), FileTypeUtils.getExtension(file), null);
        return domain + "/" + storePath.getFullPath();
    }

    /* (non-Javadoc)
     * @see com.yida.file.service.ISysFileService#download(java.lang.String, java.lang.String, java.lang.String, javax.servlet.http.HttpServletResponse)
     */
    @Override
    public InputStream download(String filePath, String bucket, String downloadName, HttpServletResponse response) throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see com.yida.file.service.ISysFileService#uploadFileDynamicLink(org.springframework.web.multipart.MultipartFile, java.lang.String, java.lang.String, boolean)
     */
    @Override
    public SysFile uploadFileDynamicLink(MultipartFile file, String bucketName, String fileName, boolean dynamicLinkFlag) throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

}
